package com.cjf.androidstudy;

import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.cjf.androidstudy.component.BootCompletedReceiver;
import com.cjf.androidstudy.component.BroadcastReceiverActivity;
import com.cjf.androidstudy.component.ComponentActivity;
import com.cjf.androidstudy.component.ShutdownBroadcastReceiver;
import com.cjf.androidstudy.component.fragment.FragmentActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    // 可以使用快捷键logt 生成下面的内容
    private static final String TAG = "MainActivity";
    /**
     * 剪切板管理器
     */
    private ClipboardManager clipboardManager;
    private Button componentButton;
    private Button broadcastReceiverButton;
    private Button fragmentButton;

    private BootCompletedReceiver bootCompletedReceiver;
    private ShutdownBroadcastReceiver shutdownBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 隐藏标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        Log.i(TAG, "onCreate: 主页面");
        bootCompletedReceiver = new BootCompletedReceiver();
        shutdownBroadcastReceiver = new ShutdownBroadcastReceiver();
        registerReceiver(bootCompletedReceiver, new IntentFilter(Intent.ACTION_BOOT_COMPLETED));
        registerReceiver(shutdownBroadcastReceiver, new IntentFilter(Intent.ACTION_SHUTDOWN));

        initView();
    }

    private void initView() {
        componentButton = findViewById(R.id.componentButton);
        broadcastReceiverButton = findViewById(R.id.broadcastReceiverButton);
        fragmentButton = findViewById(R.id.fragmentButton);
        componentButton.setOnClickListener(this);
        broadcastReceiverButton.setOnClickListener(this);
        fragmentButton.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        clipboardManager = (ClipboardManager) getApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE);
        ClipboardManager.OnPrimaryClipChangedListener onPrimaryClipChangedListener = new ClipboardManager.OnPrimaryClipChangedListener() {
            @Override
            public void onPrimaryClipChanged() {
                ClipData primaryClip = clipboardManager.getPrimaryClip();
                if (primaryClip != null && primaryClip.getItemCount() > 0) {
                    ClipDescription description = primaryClip.getDescription();
                    System.out.println("mime:===" + description.getMimeType(0));
                    CharSequence text = primaryClip.getItemAt(0).getText();
                    System.out.println("粘贴的内容" + text);
                }
            }
        };
        clipboardManager.addPrimaryClipChangedListener(onPrimaryClipChangedListener);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.componentButton:
                Intent intent = new Intent(MainActivity.this, ComponentActivity.class);
                startActivity(intent);
                break;
            case R.id.broadcastReceiverButton:
                startActivity(new Intent(MainActivity.this, BroadcastReceiverActivity.class));
                break;
            case R.id.fragmentButton:
                startActivity(new Intent(MainActivity.this, FragmentActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(bootCompletedReceiver);
        unregisterReceiver(shutdownBroadcastReceiver);
    }

    private void replaceFragment(Fragment fragment) {

    }
}
